﻿using Sustainalytics.LongRunningTaskManager.Interface;
using System.Collections.Generic;
using System.Linq;

namespace Sustainalytics.LongRunningTaskManager
{
    /// <summary>
    /// Task result as a file which contents are stored in memory.
    /// </summary>
    public class LongRunningTaskFileInMemResult : ILongRunningTaskResult, IEqualityComparer<LongRunningTaskFileInMemResult>
    {
        /// <summary>
        /// Task result type.
        /// </summary>
        public LongRunningTaskResultTypeEnum Type
        {
            get { return LongRunningTaskResultTypeEnum.FileInMem; }
        }

        /// <summary>
        /// File contents.
        /// </summary>
        public byte[] Contents { get; set; }

        /// <summary>
        /// Specify if no transformations should be run against current result.
        /// <example> 
        /// Current result may be stored to disk if is false or may be stored in memory if is true. 
        /// Therefore in some cases may be more viable to store the contents in memory compared to storing the contents on disk.
        /// </example>
        /// </summary>
        public bool EnforceInMemory { get; set; }

        /// <summary>
        /// Prferred file name.
        /// </summary>
        public string FileName { get; set; }

        /// <summary>
        /// MIME Type.
        /// </summary>
        public string MimeType { get; set; }


        public bool Equals(ILongRunningTaskResult x, ILongRunningTaskResult y)
        {
            if (x == null && y == null)
                return true;

            if (x == null || y == null)
                return false;

            if (x.GetType() != typeof(LongRunningTaskFileInMemResult) || x.GetType() != y.GetType())
                return false;

            return this.Equals((LongRunningTaskFileInMemResult)x, (LongRunningTaskFileInMemResult)y);
        }

        public int GetHashCode(ILongRunningTaskResult obj)
        {
            return this.GetHashCode(obj);
        }


        public bool Equals(LongRunningTaskFileInMemResult x, LongRunningTaskFileInMemResult y)
        {
            if (x == null && y == null)
                return true;

            if (x == null || y == null)
                return false;

            return ((x.Contents == null && y.Contents == null) || (x.Contents != null && y.Contents != null && x.Contents.SequenceEqual(y.Contents)))
                && x.EnforceInMemory == y.EnforceInMemory
                && ((x.FileName == null || y.FileName == null) || x.FileName == y.FileName)
                && ((x.MimeType == null || y.MimeType == null) || x.MimeType == y.MimeType);
        }

        public int GetHashCode(LongRunningTaskFileInMemResult obj)
        {
            return 0;
        }
    }
}
